/**
 * 简化弹窗函数
 * 
 * @author QJ
 */
// 警告
function showAlert(msg) {
	$.messager.alert("警告", msg);
}

// alert类型的消息
function showMsg(msg) {
	$.messager.alert("信息", msg);
}

// 信息
function showInfo(msg) {
	$.messager.show({
		title : "信息",
		msg : msg,
		icon : "info",
		position : "bottomRight"
	});
}
/**
 * 警告信息
 * 
 * @param msg
 */
function showWarnInfo(msg, position) {
	$.messager.show({
		title : "警告",
		msg : msg,
		icon : "warn",
		position : position ? position : "bottomRight"
	});
}

/**
 * 左下角提示
 * 
 * @param json
 *            参考Java中的Json类
 */
function showInfoForCallBack(json) {
	$.messager.show({
		msg : json.msg,
		icon : json.success ? "info" : "warning",
		position : json.success ? "bottomRight" : "topCenter",
		timeout : json.success ? 5000 : 0,
		maximizable : !json.success,
		draggable : !json.success
	});
}
// 确认
function showConfirm(msg, callback) {
	$.messager.confirm("确认", msg, function(r) {
		if (r) {
			try {
				if (jQuery.isFunction(callback)) {
					callback.call();
				}
			} catch (e) {

			}
		}
	});
}
/**
 * 删除选中项(用与多选)
 * 
 * @author QJ
 * @param tableId
 * @param url
 */
function DeleteChecked(tableId, deleteUrl) {
	var rows = $(tableId).datagrid("getSelections");
	if (rows == null || rows.length == 0) {
		showAlert("您未选中任何记录!");
	} else {
		showConfirm("您确定删除选中记录?删除后将不可恢复!", function() {
			$.each(rows, function(index, row) {
				$.ajax({
					url : deleteUrl + "/" + row.id,
					async : false
				});
			});
			showInfo("删除成功!");
			$(tableId).datagrid("reload");
		});
	}
}

/**
 * 简化DataGrid的删除操作
 * 
 * @author QJ
 * @param tableId
 * @param url
 */
function GridDelete(gridId, deleteUrl) {
	var row = $(gridId).datagrid("getSelected");
	if (row == null) {
		showAlert("您未选择任何记录!");
	} else {
		showConfirm("您确定删除该记录?删除后将不可恢复!", function() {
			ajaxGet(deleteUrl + "/" + row.id, function() {
				$(gridId).datagrid("reload");
			});
		});
	}
}
/**
 * 简化TreeGrid的删除操作
 * 
 * @author QJ
 * @param tableId
 * @param url
 */
function TreeGridDelete(tableId, deleteUrl) {
	var row = $(tableId).treegrid("getSelected");
	if (row == null) {
		showAlert("您未选择任何记录!");
	} else {
		showConfirm("您确定删除该记录?删除后将不可恢复!", function() {
			ajaxGet(deleteUrl + "/" + row.id, function() {
				$(tableId).treegrid("reload");
			});
		});
	}
}
/**
 * 删除树中勾选的所有叶子节点
 * 
 * @param $tree
 * @param deleteUrl
 */
function multiDeleteTreeNode($tree, deleteUrl) {
	var nodes = $tree.tree('getChecked');
	var nodeIds = "";
	if (nodes != null) {
		$.each(nodes, function(index, value) {
			if ($tree.tree("isLeaf", value.target)) {
				nodeIds += (value.attributes.id + ",")
			}
		});
		if (nodeIds == "") {
			$.messager.alert("操作提醒", "请先勾选需要删除的叶子节点", "warning");
		} else {
			$.messager.confirm("您确定要删除所选节点吗？", function(c) {
				if (c) {
					$.post(deleteUrl, {
						ids : nodeIds
					}, function(result) {
						$.messager.show({
							msg : result.msg,
							icon : "info",
							position : "bottomRight"
						});
						if (result.success) {
							$.each(nodes, function(index, value) {
								if ($tree.tree("isLeaf", value.target)) {
									$tree.tree("remove", value.target);
								}
							});
						}
					}, 'json');
				}
			});
		}
	} else {
		$.messager.alert("操作提醒", "请先勾选需要删除的叶子节点", "warning");
	}
}
/**
 * 删除DataGrid勾选的所有记录
 * 
 * @param $tree
 * @param deleteUrl
 */
function multiDeleteDataGrid($grid, deleteUrl, $childgrid, operName) {
	var rows = $grid.datagrid('getChecked');
	var ids = "";
	operName = operName ? operName : "删除";
	if (rows) {
		$.each(rows, function(index, value) {
			ids += (value.id + ",")
		});
		if (ids == "") {
			$.messager.alert("操作提醒", "请先勾选需要" + operName + "的记录", "warning");
		} else {
			$.messager.confirm("您确定要" + operName + "勾选的" + rows.length + "条记录吗？", function(c) {
				if (c) {
					$.post(deleteUrl, {
						ids : ids
					}, function(result) {
						showInfo(result.msg);
						if (result.success) {
							$grid.datagrid("reload").datagrid("clearSelections").datagrid("clearChecked");
							if ($childgrid) {
								$childgrid.datagrid("reload");
							}

						}
					}, 'json');
				}
			});
		}
	} else {
		$.messager.alert("操作提醒", "请先勾选需要" + operName + "的记录", "warning");
	}
}
/**
 * 删除明细DataGrid勾选的所有记录，刷新主表FORM
 * 
 */
function multiDeleteDataGridCallback($grid, deleteUrl, $childgrid, operName) {
	var rows = $grid.datagrid('getChecked');
	var ids = "";
	operName = operName ? operName : "删除";
	if (rows) {
		$.each(rows, function(index, value) {
			ids += (value.id + ",")
		});
		if (ids == "") {
			$.messager.alert("操作提醒", "请先勾选需要" + operName + "的记录", "warning");
		} else {
			$.messager.confirm("您确定要" + operName + "勾选的" + rows.length + "条记录吗？", function(c) {
				if (c) {
					$.post(deleteUrl, {
						ids : ids
					}, function(result) {
						showInfo(result.msg);
						if (result.success) {
							$grid.datagrid("reload").datagrid("clearSelections").datagrid("clearChecked");
							if ($("#fm")) {
								$("#fm").form("load", result.obj);
							}

							if ($childgrid) {
								$childgrid.datagrid("reload");
							}

						}
					}, 'json');
				}
			});
		}
	} else {
		$.messager.alert("操作提醒", "请先勾选需要" + operName + "的记录", "warning");
	}
}
/**
 * 删除TreeGrid勾选的所有记录
 * 
 * @param $treegrid
 * @param deleteUrl
 */
function multiDeleteTreeGrid($treegrid, deleteUrl) {
	var rows = $treegrid.treegrid('getChecked');
	var ids = "";
	if (rows) {
		$.each(rows, function(index, value) {
			if ($treegrid.treegrid("getChildren", value.id) == "") {

				ids += (value.id + ",");
			}
		});
		if (ids == "") {
			$.messager.alert("操作提醒", "请先勾选需要删除的叶子节点记录", "warning");
		} else {
			$.messager.confirm("您确定要删除勾选的所有记录吗？", function(c) {
				if (c) {
					$.post(deleteUrl, {
						ids : ids
					}, function(result) {
						showInfo(result.msg);
						if (result.success) {
							$treegrid.treegrid("reload").treegrid("clearSelections").treegrid("clearChecked");
						}
					}, 'json');
				}
			});
		}
	} else {
		$.messager.alert("操作提醒", "请先勾选需要删除的记录", "warning");
	}
}

/**
 * 打印报表
 * 
 * @param businessStyle
 * @param $grid
 * @param url
 */
function printReport($grid, businessStyle, url) {
	var rowData = $grid.datagrid("getSelected");
	$.get(kl.ctx + "/hgdata/printRecord/getPrintNumber", {
		businessStyle : businessStyle,
		businessId : rowData.id
	}, function(data) {
		if (data.success) {
			$.messager.confirm("确认", data.msg, function(r) {
				if (r) {
					window.open(url + rowData.id, "_blank");
				}
			});
		}
	}, "json");
}

/**
 * Form表单提交保存
 * 
 * @param formId
 * @param dataGridId
 * @param dlgId
 * @param url
 * @param callback
 */
function formSave(formId, dataGridId, dlgId, url, callback) {
	var $submitBtn = $(event.target).button({
		loadingText : '加载中...'
	}).button('loading');
	if(!($(formId).form("validate"))){
		$submitBtn.button('reset');
		return;
	}
	$(formId).form("submit", {
		url : url,
		success : function(data) {
			$submitBtn.button('reset');
			if (callback) {
				callback(data);
			} else {
				if (data.success) {
					$(dataGridId).datagrid("reload");
					$(dlgId).dialog('close');
				}
				showInfoForCallBack(data);
			}
		}
	});
}
/**
 * 获取工具栏中所有搜索条件
 * 
 * @param toolbarId
 * @returns {___anonymous3233_3234}
 */
function getSearchConditions(toolbarId) {
	var tb = toolbarId || "#tt";
	var obj = {};
	$(tb).find("input").each(function() {
		var $this = $(this);
		if ($this.val() != "") {
			var key = $this.attr("name");
			obj[key] = $this.val();
		}
	});
	return obj;
}

function checkAll($tree) {
	$($tree.tree("getRoots")).each(function(index, value) {
		$tree.tree("check", value.target);
	});
}
function unCheckAll($tree) {
	$($tree.tree("getRoots")).each(function(index, value) {
		$tree.tree("uncheck", value.target);
	});
}

/**
 * 是否启用fommater，显示 是/否
 * 
 * @author QJ
 */
function fmt_enable(val) {
	if (val == "Y" || val == "yes" || val == "1" || val == "true" || val == "True" || val == true) {
		return '<span style="color:blue">是</span>';
	} else {
		return '<span style="color:red">否</span>';
	}
}

/**
 * 是否启用fommater，显示一个Switch Button
 * 
 * @author QJ
 */
function fmt_switchBtn(value, row, index) {
	return '<input class="easyui-switchbutton "' + (value == 1 ? "checked" : "")
			+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
}

/**
 * 是否启用fommater,显示一个checkbox
 * 
 * @author QJ
 */
function fmt_check(val) {
	if (val == "Y" || val == "yes" || val == "1" || val == "true" || val == "True" || val == true) {
		return '<input type="checkbox" checked="true" onclick="return false">';
	} else {
		return '<input type="checkbox" onclick="return false">';
	}
}

/**
 * 确认状态
 * 
 * @author QJ
 */
function fmt_confirmFlag(val) {
	if (val == 0) {
		return '草稿'
	}
	if (val == 1) {
		return '<span style="color:#3333cc">已确认</span>'
	}
	if (val == 2) {
		return '<span style="color:#090">审批中</span>'
	}
	if (val == -1) {
		return '<span style="color:#F00">撤销</span>'
	}
}

/**
 * 确认状态CellStyler
 * 
 * @param value
 * @param row
 * @param index
 * @returns {String}
 */
function styler_confirm(value, row, index) {
	if (!row)
		return;
	if (row.confirmFlag == 1) {
		return 'background-color:#eef3f8;';
	}
	if (row.confirmFlag == 2) {
		return 'background-color:#f2f9f2;';
	}
	if (row.confirmFlag == -1) {
		return 'background-color:#fff6f6;';
	}
}

/**
 * 获取业务单据号
 * 
 * @param djdm
 * @param callback
 * @returns {Boolean}
 */
function fn_getDocNo(djdm, callback) {
	if (!callback) {
		$.messager.show({
			msg : '获取单据号的回调函数不能为空',
			icon : "error",
			position : "bottomRight"
		});
		return false;
	}
	$.get(kl.ctx + "/pub/getDocNo/" + djdm, function(result) {
		if (result.success) {
			callback(result.obj);
		} else {
			$.messager.show({
				msg : result.msg,
				icon : "error",
				position : "bottomRight"
			});
		}
	}, 'json');
}
/**
 * 获取中文字符串拼音首字母
 * 
 * @param cnStr
 * @param namePinyinInputID
 * @param callback
 * @returns {String}
 */
function fn_getFirstLetter(cnStr, namePinyinInputID, callback) {
	if (!cnStr) {
		return false;
	}
	$.get(kl.ctx + "/pub/getFirstLetter?cnStr=" + cnStr, function(result) {
		if (result.success) {
			if (namePinyinInputID) {// 如果指定拼音首字母对应的Input ID
				$("#" + namePinyinInputID).val(result.obj);
			} else {
				$("#namePinyin").val(result.obj);
			}
			if (callback) {// 如果回调函数不为空则调用
				callback(result.obj);
			}
		} else {
			$.messager.show({
				msg : result.msg,
				icon : "error",
				position : "bottomRight"
			});
		}
	}, 'json');
}

/**
 * 通过代码表生成下拉框
 * 
 * @param inputId
 * @param tableCode
 * @returns {Boolean}
 */
function fn_initCombo(inputId, tableCode, required, idField) {
	if (!inputId) {
		showWarnInfo("输入框ID不能为空");
		return false;
	}
	if (!tableCode) {
		showWarnInfo("代码表标识码不能为空");
		return false;
	}
	var option = {
		url : kl.ctx + '/pub/combotree/' + tableCode,
		method : 'get',
		editable : false,
		idField : idField ? idField : 'id',
		textField : 'text',
		parentField : 'pid',
		dataPlain : true
	};
	if (required) {
		$.extend(option, {
			required : required
		})
	}
	$('#' + inputId).combotree(option);
}
/**
 * 通过代码表生成下拉框，ID为名称
 * 
 * @param inputId
 * @param tableCode
 * @returns {Boolean}
 */
function fn_initComboNotId(inputId, tableCode, required, onSelect) {
	if (!inputId) {
		showWarnInfo("输入框ID不能为空");
		return false;
	}
	if (!tableCode) {
		showWarnInfo("代码表标识码不能为空");
		return false;
	}
	var option = {
		url : kl.ctx + '/pub/combobox/' + tableCode,
		method : 'get',
		editable : false,
		idField : 'text',
		textField : 'text',
		dataPlain : true,
		width : 177
	};
	if (onSelect) {
		$.extend(option, {
			onSelect : onSelect
		});
	}
	if (required) {
		$.extend(option, {
			required : required
		})
	}
	$('#' + inputId).combobox(option);
}
/**
 * 为ComboTree设置Value
 * 
 * @param inputId
 * @param tableCode
 * @returns {Boolean}
 */
function fn_setValueForCombo(inputId, id, value) {
	if (!inputId) {
		showWarnInfo("输入框ID不能为空");
		return false;
	}
	$('#' + inputId).combobox("setValue", id).combobox("setText", value);
}
/**
 * 为ComboTree设置Value
 * 
 * @param inputId
 * @param tableCode
 * @returns {Boolean}
 */
function fn_setValueForComboBox(inputId, id, value) {
	if (!inputId) {
		showWarnInfo("输入框ID不能为空");
		return false;
	}
	$('#' + inputId).combobox("setValue", id).combotree("setText", value);
}
/**
 * 为DataGrid row Filter生成"是否"的下拉框，1：是，2：否
 * 
 * @param $grid
 * @param field
 */
function buildDGFilterYesOrNoFieldOptions($grid, field, javaType, data) {
	return {
		field : field,
		type : 'combobox',
		javaType : javaType ? javaType : 'I',
		options : {
			panelHeight : 'auto',
			editable : false,
			data : data ? data : [ {
				value : '',
				text : '全部'
			}, {
				value : '1',
				text : '是'
			}, {
				value : '2',
				text : '否'
			} ],
			onChange : function(value) {
				if (value == '') {
					$grid.datagrid('removeFilterRule', field);
				} else {
					$grid.datagrid('addFilterRule', {
						field : field,
						op : 'EQ',
						value : value
					});
				}
				$grid.datagrid('doFilter');
			}
		}
	};
}

/**
 * 为DataGrid row Filter生成"是否"的下拉框，1：是，0：否
 * 
 * @param $grid
 * @param field
 */
function buildDGFilterYesOrNoFieldOptions2($grid, field, javaType, data) {
	return {
		field : field,
		type : 'combobox',
		javaType : javaType ? javaType : 'I',
				options : {
					panelHeight : 'auto',
					editable : false,
					data : data ? data : [ {
						value : '',
						text : '全部'
					}, {
						value : '1',
						text : '是'
					}, {
						value : '0',
						text : '否'
					} ],
					onChange : function(value) {
						if (value == '') {
							$grid.datagrid('removeFilterRule', field);
						} else {
							$grid.datagrid('addFilterRule', {
								field : field,
								op : 'EQ',
								value : value
							});
						}
						$grid.datagrid('doFilter');
					}
				}
	};
}

/**
 * 
 * 克隆
 * 
 * @param businessStyle
 * @param $grid
 * @param url
 */
function clonef(rowData, $fm) {
	if (!rowData) {
		showWarnInfo("您未选中行，请选择要查看的单据");
		return false;
	}

	$("#clonedlg").dialog("open").dialog("setTitle", "出库单【" + rowData.docNo + "】详细信息查看");
	$("#clonedlg form").remove();
	$fm.clone(false).prependTo("#clonedlg");
	$("#clonedlg form").attr("id", "clonefm").find("a").remove();
	$("#clonefm input").each(function() {
		var aa = String($(this).attr("class"));
		if (aa == "undefined") {
			$(this).attr("class", "easyui-validatebox");
			// alert($(this).attr("class"));
		}
	});
	$("#clonefm").form("disable");
}

/**
 * 附件链接
 * 
 * @param val
 * @param row
 * @returns {String}
 */
function fmt_attach(val, row, index) {
	return '<a href="' + kl.ctx + '/basic/attachment/download/' + row.id + '">下载</a>&nbsp;&nbsp;<a href="javascript:attach_del(\''
			+ row.fileName + '\',\'' + row.id + '\',\'' + index + '\')">删除</a>';
}

/**
 * 附件查看链接
 * 
 * @param val
 * @param row
 * @returns {String}
 */
function fmt_attachView(val, row, index) {
	return '<a href="' + kl.ctx + '/basic/attachment/download/' + row.id + '">查看</a>';
}

/**
 * 单附件列表
 * 
 * @param val
 * @param row
 * @returns {String}
 */
function fmt_singleAttach(val, row) {
	if (row.attachmentMaster && row.attachmentMaster.attachmentList.length > 0) {
		var att = row.attachmentMaster.attachmentList[0];
		return '<a class="easyui-linkbutton" iconCls="icon-hamburg-busy" onclick="attach_del(\'' + att.fileName + '\',\'' + att.id
				+ '\')">删除</a>&nbsp;&nbsp;<a class="easyui-linkbutton" iconCls="icon-cologne-cv" onclick="go(\'' + kl.ctx
				+ '/basic/attachment/download/' + att.id + '\')">' + att.fileName + '</a>';

	}
}

/**
 * 附件上传列表
 * 
 * @param val
 * @param row
 * @returns {String}
 */
function fmt_attachList(val, row) {
	if (row.attachmentMaster) {
		var iconCls = "icon-kl-arrow-green"
		if (row.attachmentMaster.attachmentList.length == 0) {
			iconCls = "icon-kl-arrow-red";
		}
		/*
		 * if(row.attachmentMaster==null){ return ""; }else{ var
		 * id=row.attachmentMaster.id; return '<a href="'+ kl.ctx
		 * +'/basic/attachment/attachList/'+ id+ '">合同附件</a>'; }
		 */
		// return '<a href="${ctx}/basic/attachment/attachList/'+
		// row.attachmentMaster.id+ '">合同附件</a>';
		return '<a class="easyui-linkbutton" iconCls="' + iconCls + '" plain="true" onclick="go(\'' + kl.ctx
				+ '/basic/attachment/attachList/' + row.attachmentMaster.id + '\')" target="_self">附件</a>';
	}
}
function go(url) {
	window.location = url;
}
/**
 * 附件删除
 * 
 * @param fileName
 * @param id
 */
function attach_del(fileName, id, index) {
	showConfirm("确定删除附件\n[ " + fileName + " ]?", function() {
		ajaxGet(kl.ctx + '/basic/attachment/delete/' + id, function() {
			grid.datagrid("reload");
			$grid.datagrid("reload");
		});
	});
}

// 审批信息查看按钮
function fmt_wfl_view(val, row) {
	if (row.processInstanceId && row.processInstanceId != -100) {
		return '<a  class="easyui-linkbutton" plain="true" iconCls="icon-cologne-edit" onclick="wfl_view(\'' + row.processInstanceId
				+ '\')" target="_self"></a>';
	}
}

// 审批信息查看页面
function wfl_view(processInstanceId) {
	$.easyui.showDialog({
		title : "查看审批信息",
		top : 0,
		left : 200,
		width : 1060,
		height : 480,
		/*topMost: true,
		iniframe:true,*/
		enableSaveButton : false,
		enableApplyButton : false,
		enableCloseButton : false,
		resizable : true,
		href : kl.ctx + "/bpm/confForm/task-viewHistory?processInstanceId=" + processInstanceId
	});
}